/*     */ package com.zimbra.cs.ldap.unboundid;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.unboundid.asn1.ASN1OctetString;
/*     */ import com.unboundid.ldap.sdk.CompareRequest;
/*     */ import com.unboundid.ldap.sdk.CompareResult;
/*     */ import com.unboundid.ldap.sdk.Control;
/*     */ import com.unboundid.ldap.sdk.DN;
/*     */ import com.unboundid.ldap.sdk.DereferencePolicy;
/*     */ import com.unboundid.ldap.sdk.LDAPConnection;
/*     */ import com.unboundid.ldap.sdk.LDAPConnectionPool;
/*     */ import com.unboundid.ldap.sdk.LDAPException;
/*     */ import com.unboundid.ldap.sdk.LDAPResult;
/*     */ import com.unboundid.ldap.sdk.ModifyRequest;
/*     */ import com.unboundid.ldap.sdk.ResultCode;
/*     */ import com.unboundid.ldap.sdk.SearchRequest;
/*     */ import com.unboundid.ldap.sdk.SearchResult;
/*     */ import com.unboundid.ldap.sdk.SearchResultEntry;
/*     */ import com.unboundid.ldap.sdk.SearchScope;
/*     */ import com.unboundid.ldap.sdk.controls.AssertionRequestControl;
/*     */ import com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl;
/*     */ import com.unboundid.ldap.sdk.controls.SimplePagedResultsControl;
/*     */ import com.unboundid.ldap.sdk.schema.Schema;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.stats.StopWatch;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.ldap.LdapException;
/*     */ import com.zimbra.cs.ldap.LdapOp;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig.ZimbraLdapConfig;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.StopIteratingException;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.ldap.ZLdapSchema;
/*     */ import com.zimbra.cs.ldap.ZModificationList;
/*     */ import com.zimbra.cs.ldap.ZMutableEntry;
/*     */ import com.zimbra.cs.ldap.ZSearchControls;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEnumeration;
/*     */ import com.zimbra.cs.stats.ZimbraPerf;
/*     */ import java.util.Arrays;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class UBIDLdapContext
/*     */   extends ZLdapContext
/*     */ {
/*  76 */   private static boolean initialized = false;
/*     */   
/*     */   private static LdapServerConfig.ZimbraLdapConfig replicaConfig;
/*     */   private static LdapServerConfig.ZimbraLdapConfig masterConfig;
/*     */   private static LDAPConnectionPool replicaConnPool;
/*     */   private static LDAPConnectionPool masterConnPool;
/*     */   private LDAPConnectionPool connPool;
/*     */   private LDAPConnection conn;
/*     */   private final boolean isZimbraLdap;
/*     */   private DereferencePolicy derefAliasPolicy;
/*     */   
/*     */   public static synchronized void init(boolean alwaysUseMaster)
/*     */     throws LdapException
/*     */   {
/*  90 */     assert (!initialized);
/*     */     
/*  92 */     if (initialized) {
/*  93 */       return;
/*     */     }
/*     */     
/*  96 */     initialized = true;
/*     */     try
/*     */     {
/*  99 */       masterConfig = new LdapServerConfig.ZimbraLdapConfig(LdapServerType.MASTER);
/* 100 */       masterConnPool = LdapConnectionPool.createConnectionPool("ZimbraMaster", masterConfig);
/*     */     }
/*     */     catch (LdapException e) {
/* 103 */       ZimbraLog.ldap.info("master is down, falling back to replica...");
/* 104 */       replicaConfig = new LdapServerConfig.ZimbraLdapConfig(LdapServerType.REPLICA);
/* 105 */       replicaConnPool = LdapConnectionPool.createConnectionPool("ZimbraReplica", replicaConfig);
/*     */       
/* 107 */       masterConfig = replicaConfig;
/* 108 */       masterConnPool = replicaConnPool;
/* 109 */       ZimbraLog.ldap.info("using replica");
/*     */     }
/*     */     
/* 112 */     if (alwaysUseMaster) {
/* 113 */       replicaConfig = masterConfig;
/* 114 */       replicaConnPool = masterConnPool;
/*     */     } else {
/* 116 */       if (replicaConfig == null) {
/* 117 */         replicaConfig = new LdapServerConfig.ZimbraLdapConfig(LdapServerType.REPLICA);
/*     */       }
/* 119 */       if (replicaConnPool == null) {
/* 120 */         replicaConnPool = LdapConnectionPool.createConnectionPool("ZimbraReplica", replicaConfig);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static synchronized void shutdown()
/*     */   {
/* 129 */     LdapConnectionPool.closeAll();
/*     */     
/* 131 */     initialized = false;
/*     */     
/* 133 */     replicaConfig = null;
/* 134 */     masterConfig = null;
/*     */     
/* 136 */     replicaConnPool = null;
/* 137 */     masterConnPool = null;
/*     */   }
/*     */   
/*     */ 
/*     */   public void debug() {}
/*     */   
/*     */ 
/*     */   static synchronized void alwaysUseMaster()
/*     */   {
/* 146 */     replicaConnPool = masterConnPool;
/*     */   }
/*     */   
/*     */ 
/*     */   public UBIDLdapContext(LdapServerType serverType, LdapUsage usage)
/*     */     throws LdapException
/*     */   {
/* 153 */     super(usage);
/* 154 */     this.isZimbraLdap = true;
/* 155 */     this.derefAliasPolicy = DereferencePolicy.NEVER;
/*     */     
/* 157 */     if (serverType.isMaster()) {
/* 158 */       this.connPool = masterConnPool;
/*     */     } else {
/* 160 */       this.connPool = replicaConnPool;
/*     */     }
/*     */     
/* 163 */     this.conn = getConnection(this.connPool);
/*     */   }
/*     */   
/*     */ 
/*     */   public UBIDLdapContext(LdapServerConfig.ExternalLdapConfig config, LdapUsage usage)
/*     */     throws LdapException
/*     */   {
/* 170 */     super(usage);
/* 171 */     this.isZimbraLdap = false;
/* 172 */     setDerefAliasPolicy(config);
/*     */     
/* 174 */     this.connPool = LdapConnectionPool.getConnPoolByConfig(config);
/* 175 */     this.conn = getConnection(this.connPool);
/*     */   }
/*     */   
/*     */   public LDAPConnection getNative()
/*     */   {
/* 180 */     return this.conn;
/*     */   }
/*     */   
/*     */ 
/*     */   private LDAPConnection getConnection(LDAPConnectionPool pool)
/*     */     throws LdapException
/*     */   {
/*     */     try
/*     */     {
/* 189 */       long start = 0L;
/* 190 */       if (this.isZimbraLdap) {
/* 191 */         start = ZimbraPerf.STOPWATCH_LDAP_DC.start();
/*     */       }
/*     */       
/* 194 */       LDAPConnection connection = UBIDLdapOperation.GET_CONNECTION.execute(this, pool);
/*     */       
/* 196 */       if (this.isZimbraLdap) {
/* 197 */         ZimbraPerf.STOPWATCH_LDAP_DC.stop(start);
/*     */       }
/*     */       
/* 200 */       LdapConnectionPool.debugCheckOut(pool, connection);
/* 201 */       return connection;
/*     */     } catch (LDAPException e) {
/* 203 */       throw mapToLdapException("unable to get connection", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public LDAPConnectionPool getConnectionPool() {
/* 208 */     String connPoolName = this.conn.getConnectionPoolName();
/* 209 */     return LdapConnectionPool.getConnPoolByName(connPoolName);
/*     */   }
/*     */   
/*     */   public String getConnectionName() {
/* 213 */     return this.conn.getConnectionName();
/*     */   }
/*     */   
/*     */   LDAPConnection getConn() {
/* 217 */     return this.conn;
/*     */   }
/*     */   
/*     */   private String getConnectedHostname() {
/* 221 */     if (this.conn != null) {
/* 222 */       return this.conn.getConnectedAddress() + ":" + this.conn.getConnectedPort();
/*     */     }
/* 224 */     return "";
/*     */   }
/*     */   
/*     */   private LdapException mapToLdapException(String message, LDAPException e) {
/* 228 */     message = message + ": ldap host=" + getConnectedHostname();
/* 229 */     if (ResultCode.TIMEOUT == e.getResultCode()) {
/* 230 */       closeContext(true);
/*     */     }
/* 232 */     if (this.isZimbraLdap) {
/* 233 */       return UBIDLdapException.mapToLdapException(message, e);
/*     */     }
/*     */     
/*     */ 
/* 237 */     return UBIDLdapException.mapToExternalLdapException(message, e);
/*     */   }
/*     */   
/*     */ 
/*     */   private void setDerefAliasPolicy(LdapServerConfig.ExternalLdapConfig config)
/*     */   {
/* 243 */     String derefPolicy = config.getDerefAliasPolicy();
/*     */     
/* 245 */     if (derefPolicy == null)
/* 246 */       this.derefAliasPolicy = DereferencePolicy.NEVER;
/* 247 */     if ("always".equalsIgnoreCase(derefPolicy)) {
/* 248 */       this.derefAliasPolicy = DereferencePolicy.ALWAYS;
/* 249 */     } else if ("never".equalsIgnoreCase(derefPolicy)) {
/* 250 */       this.derefAliasPolicy = DereferencePolicy.NEVER;
/* 251 */     } else if ("finding".equalsIgnoreCase(derefPolicy)) {
/* 252 */       this.derefAliasPolicy = DereferencePolicy.FINDING;
/* 253 */     } else if ("searching".equalsIgnoreCase(derefPolicy)) {
/* 254 */       this.derefAliasPolicy = DereferencePolicy.SEARCHING;
/*     */     } else {
/* 256 */       ZimbraLog.ldap.warn("invalid deref alias policy: " + derefPolicy + ", default to never");
/*     */       
/* 258 */       this.derefAliasPolicy = DereferencePolicy.NEVER;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void closeContext(boolean forceClose)
/*     */   {
/* 265 */     LdapConnectionPool.debugCheckIn(this.connPool, this.conn);
/*     */     
/* 267 */     UBIDLogger.beforeOp(LdapOp.REL_CONN, this.conn);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 272 */     if (forceClose) {
/* 273 */       this.connPool.releaseDefunctConnection(this.conn);
/*     */     } else {
/* 275 */       this.connPool.releaseConnection(this.conn);
/*     */     }
/*     */     
/* 278 */     this.conn = null;
/*     */   }
/*     */   
/*     */   public void createEntry(ZMutableEntry entry) throws ServiceException
/*     */   {
/*     */     try
/*     */     {
/* 285 */       UBIDLdapOperation.CREATE_ENTRY.execute(this, ((UBIDMutableEntry)entry).getNative());
/*     */     } catch (LDAPException e) {
/* 287 */       throw mapToLdapException("unable to create entry", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void createEntry(String dn, String objectClass, String[] attrs)
/*     */     throws ServiceException
/*     */   {
/* 294 */     UBIDMutableEntry entry = new UBIDMutableEntry();
/* 295 */     entry.setDN(dn);
/*     */     
/* 297 */     entry.setAttr("objectClass", objectClass);
/*     */     
/* 299 */     for (int i = 0; i < attrs.length; i += 2) {
/* 300 */       entry.setAttr(attrs[i], attrs[(i + 1)]);
/*     */     }
/*     */     
/* 303 */     createEntry(entry);
/*     */   }
/*     */   
/*     */   public void createEntry(String dn, String[] objectClasses, String[] attrs)
/*     */     throws ServiceException
/*     */   {
/* 309 */     UBIDMutableEntry entry = new UBIDMutableEntry();
/* 310 */     entry.setDN(dn);
/*     */     
/* 312 */     Set<String> ocs = new HashSet(Arrays.asList(objectClasses));
/* 313 */     entry.addAttr("objectClass", ocs);
/*     */     
/* 315 */     for (int i = 0; i < attrs.length; i += 2) {
/* 316 */       entry.setAttr(attrs[i], attrs[(i + 1)]);
/*     */     }
/*     */     
/* 319 */     createEntry(entry);
/*     */   }
/*     */   
/*     */   public ZModificationList createModificationList()
/*     */   {
/* 324 */     return new UBIDModificationList();
/*     */   }
/*     */   
/*     */ 
/*     */   public void deleteChildren(String dn)
/*     */     throws ServiceException
/*     */   {
/*     */     try
/*     */     {
/* 333 */       ZLdapFilter filter = ZLdapFilterFactory.getInstance().anyEntry();
/*     */       
/*     */ 
/* 336 */       SearchRequest searchRequest = new SearchRequest(dn, SearchScope.ONE, this.derefAliasPolicy, 0, 0, false, ((UBIDLdapFilter)filter).getNative(), new String[0]);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 345 */       searchRequest.setAttributes(new String[] { "dn" });
/*     */       
/* 347 */       SearchResult result = UBIDLdapOperation.SEARCH.execute(this, searchRequest, filter);
/*     */       
/* 349 */       List<SearchResultEntry> entries = result.getSearchEntries();
/* 350 */       for (SearchResultEntry entry : entries) {
/* 351 */         deleteEntry(entry.getDN());
/*     */       }
/*     */     } catch (LDAPException e) {
/* 354 */       throw mapToLdapException("unable to delete children", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public ZAttributes getAttributes(String dn, String[] attrs) throws LdapException
/*     */   {
/*     */     try {
/* 361 */       SearchResultEntry entry = UBIDLdapOperation.GET_ENTRY.execute(this, dn, attrs);
/*     */       
/* 363 */       if (entry == null) {
/* 364 */         throw LdapException.ENTRY_NOT_FOUND("entry not found at " + dn, null);
/*     */       }
/* 366 */       return new UBIDAttributes(entry);
/*     */     } catch (LDAPException e) {
/* 368 */       throw mapToLdapException("unable to get attributes", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public ZLdapSchema getSchema() throws LdapException
/*     */   {
/*     */     try {
/*     */       Schema schema;
/*     */       Schema schema;
/* 377 */       if (InMemoryLdapServer.isOn()) {
/* 378 */         schema = InMemoryLdapServer.getSchema("ZIMBRA_LDAP_SERVER");
/*     */       } else {
/* 380 */         schema = UBIDLdapOperation.GET_SCHEMA.execute(this);
/*     */       }
/* 382 */       return new UBIDLdapSchema(schema);
/*     */     } catch (LDAPException e) {
/* 384 */       throw mapToLdapException("unable to get schema", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void modifyAttributes(String dn, ZModificationList modList) throws LdapException
/*     */   {
/*     */     try
/*     */     {
/* 392 */       result = UBIDLdapOperation.MODIFY_ATTRS.execute(this, dn, ((UBIDModificationList)modList).getModList());
/*     */     } catch (LDAPException e) {
/*     */       LDAPResult result;
/* 395 */       throw mapToLdapException("unable to modify attributes", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean testAndModifyAttributes(String dn, ZModificationList modList, ZLdapFilter testFilter)
/*     */     throws LdapException
/*     */   {
/*     */     try
/*     */     {
/* 404 */       ModifyRequest modReq = new ModifyRequest(dn, ((UBIDModificationList)modList).getModList());
/* 405 */       modReq.addControl(new AssertionRequestControl(((UBIDLdapFilter)testFilter).getNative()));
/*     */       
/* 407 */       LDAPResult result = UBIDLdapOperation.TEST_AND_MODIFY_ATTRS.execute(this, modReq);
/* 408 */       return true;
/*     */     } catch (LDAPException e) {
/* 410 */       if (e.getResultCode() == ResultCode.ASSERTION_FAILED)
/*     */       {
/* 412 */         return false;
/*     */       }
/*     */       
/* 415 */       throw mapToLdapException("unable to test and modify attributes", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void moveChildren(String oldDn, String newDn)
/*     */     throws ServiceException
/*     */   {
/*     */     try
/*     */     {
/* 425 */       ZLdapFilter filter = ZLdapFilterFactory.getInstance().anyEntry();
/*     */       
/*     */ 
/* 428 */       SearchRequest searchRequest = new SearchRequest(oldDn, SearchScope.ONE, this.derefAliasPolicy, 0, 0, false, ((UBIDLdapFilter)filter).getNative(), new String[0]);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 437 */       searchRequest.setAttributes(new String[] { "dn" });
/*     */       
/* 439 */       SearchResult result = UBIDLdapOperation.SEARCH.execute(this, searchRequest, filter);
/*     */       
/* 441 */       List<SearchResultEntry> entries = result.getSearchEntries();
/* 442 */       for (SearchResultEntry entry : entries) {
/* 443 */         DN entryDN = entry.getParsedDN();
/* 444 */         String childDn = entryDN.toNormalizedString();
/* 445 */         String childRdn = entryDN.getRDNString();
/*     */         
/* 447 */         UBIDLdapOperation.MODIFY_DN.execute(this, childDn, childRdn, true, newDn);
/*     */       }
/*     */     } catch (LDAPException e) {
/* 450 */       throw mapToLdapException("unable to move children", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void renameEntry(String oldDn, String newDn) throws LdapException
/*     */   {
/*     */     try {
/* 457 */       DN newDN = new DN(newDn);
/* 458 */       String newRDN = newDN.getRDNString();
/* 459 */       String newSuperiorDN = newDN.getParentString();
/*     */       
/* 461 */       UBIDLdapOperation.MODIFY_DN.execute(this, oldDn, newRDN, true, newSuperiorDN);
/*     */     } catch (LDAPException e) {
/* 463 */       throw mapToLdapException("unable to rename entry", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void replaceAttributes(String dn, ZAttributes attrs) throws LdapException
/*     */   {
/* 469 */     Map<String, Object> attrMap = attrs.getAttrs();
/*     */     
/* 471 */     UBIDModificationList modList = new UBIDModificationList();
/* 472 */     modList.replaceAll(attrMap);
/* 473 */     modifyAttributes(dn, modList);
/*     */   }
/*     */   
/*     */   public void searchPaged(SearchLdapOptions searchOptions)
/*     */     throws ServiceException
/*     */   {
/* 479 */     int maxResults = searchOptions.getMaxResults();
/* 480 */     String base = searchOptions.getSearchBase();
/* 481 */     ZLdapFilter filter = searchOptions.getFilter();
/* 482 */     Set<String> binaryAttrs = searchOptions.getBinaryAttrs();
/* 483 */     SearchScope searchScope = ((UBIDSearchScope)searchOptions.getSearchScope()).getNative();
/* 484 */     SearchLdapOptions.SearchLdapVisitor visitor = searchOptions.getVisitor();
/*     */     
/* 486 */     boolean wantPartialResult = true;
/*     */     try
/*     */     {
/* 489 */       SearchRequest searchRequest = new SearchRequest(base, searchScope, this.derefAliasPolicy, maxResults, 0, false, ((UBIDLdapFilter)filter).getNative(), new String[0]);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 497 */       searchRequest.setAttributes(searchOptions.getReturnAttrs());
/*     */       
/*     */ 
/* 500 */       int pageSize = searchOptions.getResultPageSize();
/* 501 */       ASN1OctetString cookie = null;
/*     */       do
/*     */       {
/* 504 */         List<Control> controls = Lists.newArrayListWithCapacity(2);
/* 505 */         if (searchOptions.isUseControl()) {
/* 506 */           controls.add(new SimplePagedResultsControl(pageSize, cookie));
/*     */         }
/* 508 */         if (searchOptions.isManageDSAit()) {
/* 509 */           controls.add(new ManageDsaITRequestControl(false));
/*     */         }
/* 511 */         searchRequest.setControls((Control[])controls.toArray(new Control[0]));
/*     */         
/* 513 */         SearchResult result = null;
/*     */         try {
/* 515 */           result = UBIDLdapOperation.SEARCH.execute(this, searchRequest, filter);
/*     */         } catch (LDAPException e) {
/* 517 */           if ((ResultCode.SIZE_LIMIT_EXCEEDED == e.getResultCode()) && (wantPartialResult))
/*     */           {
/* 519 */             LDAPResult ldapResult = e.toLDAPResult();
/* 520 */             if ((ldapResult instanceof SearchResult)) {
/* 521 */               SearchResult searchResult = (SearchResult)ldapResult;
/* 522 */               for (SearchResultEntry entry : searchResult.getSearchEntries()) {
/* 523 */                 String dn = entry.getDN();
/* 524 */                 UBIDAttributes ubidAttrs = new UBIDAttributes(entry);
/* 525 */                 if (visitor.wantAttrMapOnVisit()) {
/* 526 */                   visitor.visit(dn, ubidAttrs.getAttrs(binaryAttrs), ubidAttrs);
/*     */                 } else {
/* 528 */                   visitor.visit(dn, ubidAttrs);
/*     */                 }
/*     */               }
/*     */             }
/*     */           }
/*     */           
/* 534 */           throw e;
/*     */         }
/*     */         
/* 537 */         for (SearchResultEntry entry : result.getSearchEntries()) {
/* 538 */           String dn = entry.getDN();
/* 539 */           UBIDAttributes ubidAttrs = new UBIDAttributes(entry);
/* 540 */           if (visitor.wantAttrMapOnVisit()) {
/* 541 */             visitor.visit(dn, ubidAttrs.getAttrs(binaryAttrs), ubidAttrs);
/*     */           } else {
/* 543 */             visitor.visit(dn, ubidAttrs);
/*     */           }
/*     */         }
/*     */         
/* 547 */         cookie = null;
/* 548 */         for (Control c : result.getResponseControls()) {
/* 549 */           if ((c instanceof SimplePagedResultsControl)) {
/* 550 */             cookie = ((SimplePagedResultsControl)c).getCookie();
/*     */           }
/*     */         }
/* 553 */         if (cookie == null) break; } while (cookie.getValueLength() > 0);
/*     */     }
/*     */     catch (SearchLdapOptions.StopIteratingException e) {}catch (LDAPException e)
/*     */     {
/* 557 */       throw mapToLdapException("unable to search ldap", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean compare(String dn, String attributeName, String assertionValue)
/*     */     throws ServiceException
/*     */   {
/* 574 */     CompareRequest compareRequest = new CompareRequest(dn, attributeName, assertionValue);
/*     */     try
/*     */     {
/* 577 */       CompareResult compareResult = UBIDLdapOperation.COMPARE.execute(this, compareRequest);
/*     */       
/*     */ 
/*     */ 
/* 581 */       return compareResult.compareMatched();
/*     */     } catch (LDAPException le) {
/* 583 */       ZimbraLog.ldap.debug("Compare failed result code='%s' error message='%s'", le.getResultCode(), le.getDiagnosticMessage(), le);
/*     */     }
/*     */     
/* 586 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */   public ZSearchResultEnumeration searchDir(String baseDN, ZLdapFilter filter, ZSearchControls searchControls)
/*     */     throws LdapException
/*     */   {
/* 593 */     UBIDSearchControls sc = (UBIDSearchControls)searchControls;
/*     */     try
/*     */     {
/* 596 */       SearchRequest searchRequest = new SearchRequest(baseDN, sc.getSearchScope(), this.derefAliasPolicy, sc.getSizeLimit(), sc.getTimeLimit(), sc.getTypesOnly(), ((UBIDLdapFilter)filter).getNative(), new String[0]);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 604 */       searchRequest.setAttributes(sc.getReturnAttrs());
/*     */       
/* 606 */       SearchResult result = UBIDLdapOperation.SEARCH.execute(this, searchRequest, filter);
/*     */       
/* 608 */       return new UBIDSearchResultEnumeration(result);
/*     */     } catch (LDAPException e) {
/* 610 */       throw mapToLdapException("unable to search ldap", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public long countEntries(String baseDN, ZLdapFilter filter, ZSearchControls searchControls)
/*     */     throws LdapException
/*     */   {
/* 618 */     UBIDSearchControls sc = (UBIDSearchControls)searchControls;
/*     */     try
/*     */     {
/* 621 */       SearchRequest searchRequest = new SearchRequest(baseDN, sc.getSearchScope(), this.derefAliasPolicy, sc.getSizeLimit(), sc.getTimeLimit(), sc.getTypesOnly(), ((UBIDLdapFilter)filter).getNative(), new String[0]);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 629 */       NoopSearchControl noopSearchCtl = new NoopSearchControl();
/* 630 */       searchRequest.addControl(noopSearchCtl);
/*     */       
/*     */ 
/*     */ 
/* 634 */       if (sc.getReturnAttrs() != null) {
/* 635 */         throw LdapException.INVALID_REQUEST("return attributes are not allowed for countEntries", null);
/*     */       }
/*     */       
/* 638 */       SearchResult result = UBIDLdapOperation.SEARCH.execute(this, searchRequest, filter);
/*     */       
/* 640 */       NoopSearchControl control = NoopSearchControl.get(result);
/* 641 */       if (control == null) {
/* 642 */         throw LdapException.LDAP_ERROR("Noop search control is not present in response", null);
/*     */       }
/* 644 */       return control.getCount();
/*     */     } catch (LDAPException e) {
/* 646 */       throw mapToLdapException("unable to search ldap", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void deleteEntry(String dn) throws LdapException
/*     */   {
/*     */     try {
/* 653 */       UBIDLdapOperation.DELETE_ENTRY.execute(this, dn);
/*     */     } catch (LDAPException e) {
/* 655 */       throw mapToLdapException("unable delete", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void setPassword(String dn, String newPassword) throws LdapException
/*     */   {
/*     */     try {
/* 662 */       UBIDLdapOperation.SET_PASSWORD.execute(this, dn, newPassword);
/*     */     } catch (LDAPException e) {
/* 664 */       throw mapToLdapException("unable to change password", e);
/*     */     }
/*     */   }
/*     */   
/*     */   /* Error */
/*     */   private static void ldapAuthenticate(com.zimbra.cs.ldap.LdapServerConfig config, String bindDN, String password, LdapUsage usage)
/*     */     throws ServiceException
/*     */   {
/*     */     // Byte code:
/*     */     //   0: iconst_0
/*     */     //   1: istore 4
/*     */     //   3: new 220	com/zimbra/cs/ldap/unboundid/LdapServerPool
/*     */     //   6: dup
/*     */     //   7: aload_0
/*     */     //   8: invokespecial 221	com/zimbra/cs/ldap/unboundid/LdapServerPool:<init>	(Lcom/zimbra/cs/ldap/LdapServerConfig;)V
/*     */     //   11: astore 5
/*     */     //   13: aconst_null
/*     */     //   14: astore 6
/*     */     //   16: aconst_null
/*     */     //   17: astore 7
/*     */     //   19: getstatic 222	com/zimbra/cs/ldap/unboundid/UBIDLdapOperation:GENERIC_OP	Lcom/zimbra/cs/ldap/unboundid/UBIDLdapOperation$GenericOp;
/*     */     //   22: invokevirtual 223	com/zimbra/cs/ldap/unboundid/UBIDLdapOperation$GenericOp:begin	()J
/*     */     //   25: lstore 8
/*     */     //   27: invokestatic 120	com/zimbra/cs/ldap/unboundid/InMemoryLdapServer:isOn	()Z
/*     */     //   30: ifeq +16 -> 46
/*     */     //   33: invokestatic 224	com/zimbra/cs/ldap/unboundid/InMemoryLdapServer:getConnection	()Lcom/unboundid/ldap/sdk/LDAPConnection;
/*     */     //   36: astore 6
/*     */     //   38: aload_2
/*     */     //   39: invokestatic 225	com/zimbra/cs/ldap/unboundid/InMemoryLdapServer$Password:treatPassword	(Ljava/lang/String;)Ljava/lang/String;
/*     */     //   42: astore_2
/*     */     //   43: goto +13 -> 56
/*     */     //   46: aload 5
/*     */     //   48: invokevirtual 226	com/zimbra/cs/ldap/unboundid/LdapServerPool:getServerSet	()Lcom/unboundid/ldap/sdk/ServerSet;
/*     */     //   51: invokevirtual 227	com/unboundid/ldap/sdk/ServerSet:getConnection	()Lcom/unboundid/ldap/sdk/LDAPConnection;
/*     */     //   54: astore 6
/*     */     //   56: aload 5
/*     */     //   58: invokevirtual 228	com/zimbra/cs/ldap/unboundid/LdapServerPool:getConnectionType	()Lcom/zimbra/cs/ldap/LdapConnType;
/*     */     //   61: getstatic 229	com/zimbra/cs/ldap/LdapConnType:STARTTLS	Lcom/zimbra/cs/ldap/LdapConnType;
/*     */     //   64: if_acmpne +46 -> 110
/*     */     //   67: aload_0
/*     */     //   68: invokevirtual 230	com/zimbra/cs/ldap/LdapServerConfig:sslAllowUntrustedCerts	()Z
/*     */     //   71: invokestatic 231	com/zimbra/cs/ldap/unboundid/LdapSSLUtil:createSSLContext	(Z)Ljavax/net/ssl/SSLContext;
/*     */     //   74: astore 10
/*     */     //   76: aload 6
/*     */     //   78: new 232	com/unboundid/ldap/sdk/extensions/StartTLSExtendedRequest
/*     */     //   81: dup
/*     */     //   82: aload 10
/*     */     //   84: invokespecial 233	com/unboundid/ldap/sdk/extensions/StartTLSExtendedRequest:<init>	(Ljavax/net/ssl/SSLContext;)V
/*     */     //   87: invokevirtual 234	com/unboundid/ldap/sdk/LDAPConnection:processExtendedOperation	(Lcom/unboundid/ldap/sdk/ExtendedRequest;)Lcom/unboundid/ldap/sdk/ExtendedResult;
/*     */     //   90: astore 11
/*     */     //   92: aload 11
/*     */     //   94: invokevirtual 235	com/unboundid/ldap/sdk/ExtendedResult:getResultCode	()Lcom/unboundid/ldap/sdk/ResultCode;
/*     */     //   97: getstatic 236	com/unboundid/ldap/sdk/ResultCode:SUCCESS	Lcom/unboundid/ldap/sdk/ResultCode;
/*     */     //   100: if_acmpeq +10 -> 110
/*     */     //   103: ldc -19
/*     */     //   105: aconst_null
/*     */     //   106: invokestatic 238	com/zimbra/common/service/ServiceException:FAILURE	(Ljava/lang/String;Ljava/lang/Throwable;)Lcom/zimbra/common/service/ServiceException;
/*     */     //   109: athrow
/*     */     //   110: aload 6
/*     */     //   112: aload_1
/*     */     //   113: aload_2
/*     */     //   114: invokevirtual 239	com/unboundid/ldap/sdk/LDAPConnection:bind	(Ljava/lang/String;Ljava/lang/String;)Lcom/unboundid/ldap/sdk/BindResult;
/*     */     //   117: astore 7
/*     */     //   119: aload 7
/*     */     //   121: invokevirtual 240	com/unboundid/ldap/sdk/BindResult:getResultCode	()Lcom/unboundid/ldap/sdk/ResultCode;
/*     */     //   124: getstatic 236	com/unboundid/ldap/sdk/ResultCode:SUCCESS	Lcom/unboundid/ldap/sdk/ResultCode;
/*     */     //   127: if_acmpeq +10 -> 137
/*     */     //   130: ldc -15
/*     */     //   132: aconst_null
/*     */     //   133: invokestatic 238	com/zimbra/common/service/ServiceException:FAILURE	(Ljava/lang/String;Ljava/lang/Throwable;)Lcom/zimbra/common/service/ServiceException;
/*     */     //   136: athrow
/*     */     //   137: iconst_1
/*     */     //   138: istore 4
/*     */     //   140: getstatic 222	com/zimbra/cs/ldap/unboundid/UBIDLdapOperation:GENERIC_OP	Lcom/zimbra/cs/ldap/unboundid/UBIDLdapOperation$GenericOp;
/*     */     //   143: getstatic 242	com/zimbra/cs/ldap/LdapOp:OPEN_CONN	Lcom/zimbra/cs/ldap/LdapOp;
/*     */     //   146: aload_3
/*     */     //   147: lload 8
/*     */     //   149: iload 4
/*     */     //   151: aload 7
/*     */     //   153: ldc -13
/*     */     //   155: iconst_4
/*     */     //   156: anewarray 244	java/lang/Object
/*     */     //   159: dup
/*     */     //   160: iconst_0
/*     */     //   161: aload 6
/*     */     //   163: ifnonnull +8 -> 171
/*     */     //   166: ldc -11
/*     */     //   168: goto +11 -> 179
/*     */     //   171: aload 6
/*     */     //   173: invokevirtual 246	com/unboundid/ldap/sdk/LDAPConnection:getConnectionID	()J
/*     */     //   176: invokestatic 247	java/lang/Long:valueOf	(J)Ljava/lang/Long;
/*     */     //   179: aastore
/*     */     //   180: dup
/*     */     //   181: iconst_1
/*     */     //   182: aload 5
/*     */     //   184: invokevirtual 248	com/zimbra/cs/ldap/unboundid/LdapServerPool:getRawUrls	()Ljava/lang/String;
/*     */     //   187: aastore
/*     */     //   188: dup
/*     */     //   189: iconst_2
/*     */     //   190: aload 5
/*     */     //   192: invokevirtual 228	com/zimbra/cs/ldap/unboundid/LdapServerPool:getConnectionType	()Lcom/zimbra/cs/ldap/LdapConnType;
/*     */     //   195: invokevirtual 249	com/zimbra/cs/ldap/LdapConnType:name	()Ljava/lang/String;
/*     */     //   198: aastore
/*     */     //   199: dup
/*     */     //   200: iconst_3
/*     */     //   201: aload_1
/*     */     //   202: aastore
/*     */     //   203: invokestatic 250	java/lang/String:format	(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;
/*     */     //   206: invokevirtual 251	com/zimbra/cs/ldap/unboundid/UBIDLdapOperation$GenericOp:end	(Lcom/zimbra/cs/ldap/LdapOp;Lcom/zimbra/cs/ldap/LdapUsage;JZLcom/unboundid/ldap/protocol/LDAPResponse;Ljava/lang/String;)V
/*     */     //   209: aload 6
/*     */     //   211: ifnull +121 -> 332
/*     */     //   214: getstatic 252	com/zimbra/cs/ldap/LdapOp:CLOSE_CONN	Lcom/zimbra/cs/ldap/LdapOp;
/*     */     //   217: aload 6
/*     */     //   219: invokestatic 74	com/zimbra/cs/ldap/unboundid/UBIDLogger:beforeOp	(Lcom/zimbra/cs/ldap/LdapOp;Lcom/unboundid/ldap/sdk/LDAPConnection;)V
/*     */     //   222: aload 6
/*     */     //   224: invokevirtual 253	com/unboundid/ldap/sdk/LDAPConnection:close	()V
/*     */     //   227: goto +105 -> 332
/*     */     //   230: astore 10
/*     */     //   232: ldc -2
/*     */     //   234: aload 10
/*     */     //   236: invokestatic 59	com/zimbra/cs/ldap/unboundid/UBIDLdapException:mapToExternalLdapException	(Ljava/lang/String;Lcom/unboundid/ldap/sdk/LDAPException;)Lcom/zimbra/cs/ldap/LdapException;
/*     */     //   239: athrow
/*     */     //   240: astore 12
/*     */     //   242: getstatic 222	com/zimbra/cs/ldap/unboundid/UBIDLdapOperation:GENERIC_OP	Lcom/zimbra/cs/ldap/unboundid/UBIDLdapOperation$GenericOp;
/*     */     //   245: getstatic 242	com/zimbra/cs/ldap/LdapOp:OPEN_CONN	Lcom/zimbra/cs/ldap/LdapOp;
/*     */     //   248: aload_3
/*     */     //   249: lload 8
/*     */     //   251: iload 4
/*     */     //   253: aload 7
/*     */     //   255: ldc -13
/*     */     //   257: iconst_4
/*     */     //   258: anewarray 244	java/lang/Object
/*     */     //   261: dup
/*     */     //   262: iconst_0
/*     */     //   263: aload 6
/*     */     //   265: ifnonnull +8 -> 273
/*     */     //   268: ldc -11
/*     */     //   270: goto +11 -> 281
/*     */     //   273: aload 6
/*     */     //   275: invokevirtual 246	com/unboundid/ldap/sdk/LDAPConnection:getConnectionID	()J
/*     */     //   278: invokestatic 247	java/lang/Long:valueOf	(J)Ljava/lang/Long;
/*     */     //   281: aastore
/*     */     //   282: dup
/*     */     //   283: iconst_1
/*     */     //   284: aload 5
/*     */     //   286: invokevirtual 248	com/zimbra/cs/ldap/unboundid/LdapServerPool:getRawUrls	()Ljava/lang/String;
/*     */     //   289: aastore
/*     */     //   290: dup
/*     */     //   291: iconst_2
/*     */     //   292: aload 5
/*     */     //   294: invokevirtual 228	com/zimbra/cs/ldap/unboundid/LdapServerPool:getConnectionType	()Lcom/zimbra/cs/ldap/LdapConnType;
/*     */     //   297: invokevirtual 249	com/zimbra/cs/ldap/LdapConnType:name	()Ljava/lang/String;
/*     */     //   300: aastore
/*     */     //   301: dup
/*     */     //   302: iconst_3
/*     */     //   303: aload_1
/*     */     //   304: aastore
/*     */     //   305: invokestatic 250	java/lang/String:format	(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;
/*     */     //   308: invokevirtual 251	com/zimbra/cs/ldap/unboundid/UBIDLdapOperation$GenericOp:end	(Lcom/zimbra/cs/ldap/LdapOp;Lcom/zimbra/cs/ldap/LdapUsage;JZLcom/unboundid/ldap/protocol/LDAPResponse;Ljava/lang/String;)V
/*     */     //   311: aload 6
/*     */     //   313: ifnull +16 -> 329
/*     */     //   316: getstatic 252	com/zimbra/cs/ldap/LdapOp:CLOSE_CONN	Lcom/zimbra/cs/ldap/LdapOp;
/*     */     //   319: aload 6
/*     */     //   321: invokestatic 74	com/zimbra/cs/ldap/unboundid/UBIDLogger:beforeOp	(Lcom/zimbra/cs/ldap/LdapOp;Lcom/unboundid/ldap/sdk/LDAPConnection;)V
/*     */     //   324: aload 6
/*     */     //   326: invokevirtual 253	com/unboundid/ldap/sdk/LDAPConnection:close	()V
/*     */     //   329: aload 12
/*     */     //   331: athrow
/*     */     //   332: return
/*     */     // Line number table:
/*     */     //   Java source line #705	-> byte code offset #0
/*     */     //   Java source line #706	-> byte code offset #3
/*     */     //   Java source line #707	-> byte code offset #13
/*     */     //   Java source line #708	-> byte code offset #16
/*     */     //   Java source line #710	-> byte code offset #19
/*     */     //   Java source line #712	-> byte code offset #27
/*     */     //   Java source line #713	-> byte code offset #33
/*     */     //   Java source line #714	-> byte code offset #38
/*     */     //   Java source line #716	-> byte code offset #46
/*     */     //   Java source line #719	-> byte code offset #56
/*     */     //   Java source line #720	-> byte code offset #67
/*     */     //   Java source line #722	-> byte code offset #76
/*     */     //   Java source line #730	-> byte code offset #92
/*     */     //   Java source line #731	-> byte code offset #103
/*     */     //   Java source line #736	-> byte code offset #110
/*     */     //   Java source line #737	-> byte code offset #119
/*     */     //   Java source line #738	-> byte code offset #130
/*     */     //   Java source line #740	-> byte code offset #137
/*     */     //   Java source line #744	-> byte code offset #140
/*     */     //   Java source line #753	-> byte code offset #209
/*     */     //   Java source line #754	-> byte code offset #214
/*     */     //   Java source line #755	-> byte code offset #222
/*     */     //   Java source line #741	-> byte code offset #230
/*     */     //   Java source line #742	-> byte code offset #232
/*     */     //   Java source line #744	-> byte code offset #240
/*     */     //   Java source line #753	-> byte code offset #311
/*     */     //   Java source line #754	-> byte code offset #316
/*     */     //   Java source line #755	-> byte code offset #324
/*     */     //   Java source line #759	-> byte code offset #332
/*     */     // Local variable table:
/*     */     //   start	length	slot	name	signature
/*     */     //   0	333	0	config	com.zimbra.cs.ldap.LdapServerConfig
/*     */     //   0	333	1	bindDN	String
/*     */     //   0	333	2	password	String
/*     */     //   0	333	3	usage	LdapUsage
/*     */     //   1	251	4	succeeded	boolean
/*     */     //   11	282	5	serverPool	LdapServerPool
/*     */     //   14	311	6	connection	LDAPConnection
/*     */     //   17	237	7	bindResult	com.unboundid.ldap.sdk.BindResult
/*     */     //   25	225	8	startTime	long
/*     */     //   74	9	10	startTLSContext	javax.net.ssl.SSLContext
/*     */     //   230	5	10	e	LDAPException
/*     */     //   90	3	11	extendedResult	com.unboundid.ldap.sdk.ExtendedResult
/*     */     //   240	90	12	localObject	Object
/*     */     // Exception table:
/*     */     //   from	to	target	type
/*     */     //   27	140	230	com/unboundid/ldap/sdk/LDAPException
/*     */     //   27	140	240	finally
/*     */     //   230	242	240	finally
/*     */   }
/*     */   
/*     */   static void externalLdapAuthenticate(String[] urls, boolean wantStartTLS, String bindDN, String password, String note)
/*     */     throws ServiceException
/*     */   {
/* 764 */     LdapServerConfig.ExternalLdapConfig config = new LdapServerConfig.ExternalLdapConfig(urls, wantStartTLS, null, bindDN, password, null, note);
/*     */     
/* 766 */     ldapAuthenticate(config, bindDN, password, LdapUsage.LDAP_AUTH_EXTERNAL);
/*     */   }
/*     */   
/*     */   static void zimbraLdapAuthenticate(String bindDN, String password) throws ServiceException
/*     */   {
/* 771 */     ldapAuthenticate(replicaConfig, bindDN, password, LdapUsage.LDAP_AUTH_ZIMBRA);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/ldap/unboundid/UBIDLdapContext.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */